package com.ml.mall.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Created by kyyang on 2021/1/10 16:16
 */
public class StrUtils {

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","");
    }

    public static String generateCode(int num){
        String randomCode = "";
        // 用字符数组的方式随机
        String model = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] m = model.toCharArray();
        for (int j = 0; j < num+1; j++) {
            char c = m[(int) (Math.random() * 36)];
            // 保证六位随机数之间没有重复的
            if (randomCode.contains(String.valueOf(c))) {
                j--;
                continue;
            }
            randomCode = randomCode + c;
        }
        return randomCode;
    }

    /**
     * @description    :根据字符串，获取到集合对象
     * @author         :zxm
     * @date           :2021/5/14
     **/
    public static List<String> getListByStr(String str){
        str = str.substring(str.indexOf("[")+1, str.indexOf("]"));
        //内容为空时,返回空集合
        if(isBlank(str)){
            return new ArrayList<>();
        }
        List<String> statesArr = Arrays.stream(str.split(","))
                .map(state -> state.replaceAll(" ", ""))
                .collect(Collectors.toList());

        return statesArr;
    }

    /**
     * 判断内容是否为空
     * @param str
     * @return
     */
    private static boolean isBlank(String str) {
        if(null == str){
            return true;
        }
        for(int i = 0; i < str.length(); i++){
            if(!Character.isWhitespace(str.charAt(i))){
                return false;
            }
        }
        return true;
    }

}
